package com.keon.jianbao.ui.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.bean.DetailAppraisal;
import com.keon.jianbao.bean.Expert;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.util.JsonUtils;
import com.keon.jianbao.util.UILUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONException;

import butterknife.Bind;

/**
 * Created by keon on 2015/10/21.
 */
public class ExpertCommentActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    @Bind(R.id.expert_comment_name)
    TextView tvExpertName;
    //    @Bind(R.id.expert_comment_times)
//    TextView tvTimes;
    @Bind(R.id.expert_comments)
    RadioGroup rgExpertComments;
    @Bind(R.id.expert_comment_content)
    EditText etContent;
    @Bind(R.id.expert_comment_avatar)
    ImageView ivAvatar;
    @Bind(R.id.expert_comment_catlog)
    TextView tvCatlog;
    @Bind(R.id.expert_comment_fee)
    TextView tvFee;

    SimpleHandler expertDetailHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            try {
                Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    Expert expert = JsonUtils.fromJson(result.getContent(), Expert.class);
                    ImageLoader.getInstance().displayImage(expert.getExpert_face(), ivAvatar, UILUtils.getOptions2());
                    tvExpertName.setText(expert.getExpert_name());
                    tvCatlog.setText(detailAppraisal.getAppraisal_catalog());
                    tvFee.setText(expert.getExpert_fee() + "");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };

    int check = 0;
    int appraisalId = 0;
    int expert_id = 0;
    DetailAppraisal detailAppraisal = null;

    SimpleHandler giveStarHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            try {
                Result result = new Result(responseBody);

                if (result.getStatus() > 0) {
                    showToast(result.getTips());
                    onBackPressed();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_expert_comment;
    }

    @Override
    protected int getToolBarTitle() {
        return R.string.expert_cmomment;
    }

    @Override
    public void initView() {
        rgExpertComments.setOnCheckedChangeListener(this);
    }

    @Override
    public void initData() {
        giveStarHandler.setContext(this);
        expertDetailHandler.setContext(this);

        Bundle bundle = getIntent().getExtras();
//        Expert expert = (Expert) bundle.getSerializable("expert");
        appraisalId = bundle.getInt("appraisalId");
        expert_id = bundle.getInt("expert_id");
        detailAppraisal = (DetailAppraisal) bundle.getSerializable("detailAppraisal");

        initExpert();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        return super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_expert_comment, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.expert_commont_submit:
                if (check()) {
                    showWaitDialog();
                    Api.give_star(appraisalId, check, giveStarHandler);
                }
//                TweetPublishActivity.showToast("11");
                break;
        }
        return true;
    }

    void initExpert() {
        showWaitDialog();
        Api.expert_detail(expert_id, expertDetailHandler);
    }

    boolean check() {
        if (check == 0) {
            showToast(R.string.expert_commont_null_error);
            return false;
        }

  /*      if (StringUtils.isEmpty(etContent.getText().toString())) {
            showToast(R.string.expert_commont_content_null_error);
            return true;
        }*/

        return true;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.expert_comment_1:
                check = 1;
                break;
            case R.id.expert_comment_2:
                check = 2;
                break;
            case R.id.expert_comment_3:
                check = 3;
                break;
        }
    }
}
